<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      // 为数组的原型挂载求平均值的方法
      Array.prototype.Getavg = function () {
        if (this.length == 0) {
          return 0;
        }
        let num = 0;
        this.forEach(function (ele, index) {
          num += ele;
        });
        return num / this.length;
      };
      //   声明一个数组
      let arr = [1, 2, 3, 4, 5];
      //   调用这个方法并接受返回值
      let result = arr.Getavg();
      //   打印返回值
      console.log(result);
    </script>
  </body>
</html>
